import { _decorator, Component, Node } from 'cc';
import { PinAction } from './PinAction';
import { ElementAction } from './ElementAction';

import { ColorDate } from './ColorDate';
import { Global } from './Global';
import { DataModel } from './DataModel';
const { ccclass, property } = _decorator;

enum LayerStatus {
    HIDDEN = 0,
    VISIBLE = 1,
    BLACK_MODE = 2,
}

@ccclass('LayerAction')
export class LayerAction extends Component {
    private layer_group_id = 0;
    private layer_color: ColorDate = null;
    private layer_status: LayerStatus = LayerStatus.HIDDEN;

    start() {}

    update(deltaTime: number) {}

    public get_hole_num(): number {
        let hole_num = 0;
        this.node.children.forEach(element_node => {
            const eleAction = element_node.getComponent(ElementAction);
            if (eleAction) {
                hole_num += eleAction.get_hole_num();
            }
        });
        return hole_num;
    }

    public init_layer() {
        this.layer_group_id = DataModel.getNewGroupIndex();
        this.layer_color = DataModel.getLayerColor();
        this.node.children.forEach(element_node => {
            const eleAction = element_node.getComponent(ElementAction);
            if (eleAction) {
                eleAction.init_element(this.layer_group_id, this.layer_color);
            }
        });
    }

    public init_pin(color_pin_arr: ColorDate[]) {
        this.node.children.forEach(element_node => {
            const eleAction = element_node.getComponent(ElementAction);
            if (eleAction) {
                eleAction.init_pin(this.layer_group_id, color_pin_arr);
            }
        });
    }

    public init_hole() {
        this.node.children.forEach(element_node => {
            const eleAction = element_node.getComponent(ElementAction);
            if (eleAction) {
                eleAction.init_hole();
            }
        });
    }

    public get_pin_color(arr: PinAction[]) {
        this.node.children.forEach(element_node => {
            const eleAction = element_node.getComponent(ElementAction);
            if (eleAction) {
                eleAction.get_pin_color(arr);
            }
        });
    }

    public get_element_num(): number {
        return this.node.children.length;
    }

    public set_status(status: LayerStatus) {
        if (this.layer_status === status) {
            return; // 状态无变化，直接返回
        }

        switch (status) {
            case LayerStatus.HIDDEN:
                this.node.active = false;
                break;

            case LayerStatus.VISIBLE:
                this.node.active = true;
                this.node.children.forEach(element_node => {
                    const eleAction = element_node.getComponent(ElementAction);
                    if (!eleAction) return;
                    if (this.layer_status === LayerStatus.HIDDEN) {
                        eleAction.flash_img();
                    } else if (this.layer_status === LayerStatus.BLACK_MODE) {
                        eleAction.set_layer_bg_or_origin(false);
                        eleAction.flash_img();
                    }
                });
                break;

            case LayerStatus.BLACK_MODE:
                this.node.children.forEach(element_node => {
                    const eleAction = element_node.getComponent(ElementAction);
                    if (eleAction) eleAction.set_layer_bg_or_origin(true);
                });
                this.scheduleOnce(() => {
                    this.node.active = true;
                    this.node.children.forEach(element_node => {
                        const eleAction = element_node.getComponent(ElementAction);
                        if (eleAction) eleAction.flash_img(0.5);
                    });
                }, 1.6);
                break;
        }

        this.layer_status = status;
    }
}
